package cn.tk.dbtest;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JEditorPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JEditorPane editorPane;
	private JTextPane textPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 479, 345);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		contentPane.add(splitPane, BorderLayout.CENTER);
		
		editorPane = new JEditorPane();
		splitPane.setLeftComponent(editorPane);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		splitPane.setRightComponent(tabbedPane);
		
		textPane = new JTextPane();
		tabbedPane.addTab("\u6D88\u606F", null, textPane, null);
		
		table = new JTable();
		tabbedPane.addTab("\u7ED3\u679C", null, table, null);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setBackground(Color.LIGHT_GRAY);
		contentPane.add(toolBar, BorderLayout.NORTH);
		
		JButton button = new JButton("\u8FD0\u884C");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				executeSQL();
			}
		});
		button.setBackground(Color.WHITE);
		button.setToolTipText("\u8FD0\u884CSQL\u8BED\u53E5");
		toolBar.add(button);
	}
	
	public void executeSQL(){
		String sql=editorPane.getText().trim();
		String type="query";
		DBTest db=new DBTest();
		ResultSet rs;
		db.connect("", "", "mytest", "root", "1234");
		if(sql.indexOf("select")>=0){
			rs=db.query(sql);
			
		}else{
			db.update(sql);
		}
		
		
	}

}
